package com.example.yuntiku.Servlet;

import com.example.yuntiku.Service.ExamService;
import com.example.yuntiku.Vo.ExamVo;
import com.example.yuntiku.entity.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ExamServlet", value = "/ExamServlet")
public class ExamServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name=request.getParameter("name");
        String date=request.getParameter("date");
        String page = request.getParameter("page");
        if (page == null || page.equals("")){
            page = "1";//默认第一页
        }
        String pageSize = request.getParameter("pageSize");
        HttpSession session = request.getSession();

        String sessionAttrName = "examPageSize"; // 设置ExamServlet的session属性名
        // 获取pageSize值
        if (pageSize == null) {
            pageSize = (String) session.getAttribute(sessionAttrName);
            if (pageSize == null) {
                pageSize = "5"; // 设置默认值
            }
        } else {
            session.setAttribute(sessionAttrName, pageSize);
        }
        int size = Integer.parseInt(pageSize);//每页记录数，每页显示3条
        ExamService pagerService=new ExamService();
        List<ExamVo> list = pagerService.selectPager(name,date, Integer.parseInt(page), size);
        //查找总记录数
        int count=pagerService.selectcount(name,date);

        PageInfo<ExamVo>pageInfo=new PageInfo<>();
        pageInfo.setList(list);
        pageInfo.setSize(size);
        pageInfo.setPage(Integer.parseInt(page));
        pageInfo.setCount(count);
        request.setAttribute("pageInfo", pageInfo);
        //转发到显示页面
        request.getRequestDispatcher("Exam.jsp").forward(request,response);
    }
}